package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ListView;

import com.example.satu.quizcreator.quiz.Answer;
import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_creator.interfaces.onAnswerListItemFocusChange;
import com.example.satu.quizcreator.quiz_creator.interfaces.onFileLoad;
import com.example.satu.quizcreator.quiz_creator.interfaces.onFileRemoveClickListener;
import com.example.satu.quizcreator.quiz_creator.interfaces.onItemFocusChange;

import java.util.ArrayList;

import roboguice.fragment.RoboFragment;
import roboguice.inject.InjectView;

import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.DATA_TYPE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE.NON_FILE;
import static com.example.satu.quizcreator.quiz_creator.QuizCreatorActivity.FILE_TYPE.values;

/**
 * Created by Satu on 2014-09-05.
 */
public class Basket_answer_fragment extends RoboFragment implements onAnswerListItemFocusChange,onFileLoad,onFileRemoveClickListener {

    @InjectView(R.id.good_answer_list)
    private ListView goodAnswerList;

    @InjectView(R.id.bad_answer_list)
    private ListView  badAnswerList;

    AnswerAdapter goodAnswerAdapter;

    AnswerAdapter badAnswerAdapter;

    @InjectView(R.id.button_add_bad_answer)
    ImageButton addBadAnswerButton;

    @InjectView(R.id.button_add__good_answer)
    ImageButton addGoodAnswerButton;

    ArrayList<Answer> badAnswers = null;
    ArrayList<Answer> goodAnswers = null;

    Answer focusedAnswer;

    public Basket_answer_fragment(ArrayList<Answer> badAnswers,ArrayList<Answer> goodAnswers) {
        this.badAnswers = badAnswers;
        this.goodAnswers = goodAnswers;
    }
    public Basket_answer_fragment(){

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.basket_answer_layout,null);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        goodAnswerAdapter = new AnswerAdapter(getActivity(),R.layout.answer_widget_description,this);
        badAnswerAdapter = new AnswerAdapter(getActivity(),R.layout.answer_widget_description,this);
        if((badAnswers != null) && (goodAnswers != null)){
            for (Answer answer : goodAnswers)
                goodAnswerAdapter.add(answer);
            for (Answer answer : badAnswers)
                badAnswerAdapter.add(answer);

        }else {
            goodAnswerAdapter.add(new Answer(getResources().getString(R.string.write_answer),true));
            badAnswerAdapter.add(new Answer(getResources().getString(R.string.write_answer),false));
        }
        goodAnswerList.setAdapter(goodAnswerAdapter);
        badAnswerList.setAdapter(badAnswerAdapter);

        addGoodAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              goodAnswerAdapter.add(new Answer(getActivity().getResources().getString(R.string.write_answer),true));
            }
        });

        addBadAnswerButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                badAnswerAdapter.add(new Answer(getActivity().getResources().getString(R.string.write_answer),false));
            }
        });
    }

    public AnswerAdapter getBadAnswerAdapter() {
        return badAnswerAdapter;
    }
    public AnswerAdapter getGoodAnswerAdapter() {
        return goodAnswerAdapter;
    }
    public ArrayList<Answer> getBadAnswers(){

        ArrayList<Answer> badAnswers = new ArrayList<Answer>();
        for (int i = 0; i < badAnswerAdapter.getCount();i++){
            badAnswers.add(badAnswerAdapter.getItem(i));
        }
        return badAnswers;

    }
    public ArrayList<Answer> getGoodAnswers(){
        ArrayList<Answer> goodAnswers = new ArrayList<Answer>();
        for (int i = 0; i < goodAnswerAdapter.getCount();i++){
            goodAnswers.add(goodAnswerAdapter.getItem(i));
        }
        return goodAnswers;
    }

    @Override
    public void onListItemFocusChange(boolean focus, Answer answer) {
        focusedAnswer = answer;
        if(focus) {
            if (getActivity() instanceof onItemFocusChange)
                ((onItemFocusChange) getActivity()).onItemFocus(true, this, answer.getFile_type());
        }else {
            if (getActivity() instanceof onItemFocusChange)
                        ((onItemFocusChange) getActivity()).onItemFocus(false, null,null);
        }
    }
    @Override
    public void onObjectLoad(Bundle bundle) {
        FILE_TYPE file_type = values()[bundle.getInt(DATA_TYPE)];
        focusedAnswer.setFile_type(file_type);
        focusedAnswer.clearFileVariables();
        focusedAnswer.setFilePath(bundle.getString(QuizCreatorActivity.FILE_PATH));
/*        switch (file_type){
            case IMAGE_TYPE:
                focusedAnswer.setBitmap((android.graphics.Bitmap) bundle.getParcelable(QuizCreatorActivity.IMAGE_TYPE));
                break;
            case AUDIO_TYPE:
                focusedAnswer.setVideo_sound(bundle.getByteArray(QuizCreatorActivity.AUDIO_TYPE)) ;
                break;
            case VIDEO_TYPE:
                focusedAnswer.setVideo_sound(bundle.getByteArray(QuizCreatorActivity.VIDEO_TYPE));
                break;
            default:
                break;
        }*/
        goodAnswerAdapter.notifyDataSetChanged();
        badAnswerAdapter.notifyDataSetChanged();
    }
    @Override
    public void onFileRemoveClick() {
        focusedAnswer.clearFileVariables();
        focusedAnswer.setFile_type(NON_FILE);
    }
}
